<?php
/**
 * @package: Asalsuri
 * @subpackage: contract Model
 * @author:  Pham Van Diep
 * @date: 27/2/2014
 * 
*/
class Contract_Model extends CI_Model{
	private $__database;
	public function __construct() {
        parent::__construct();
		$this->_database = $this->db->conn_id;		
	}
    /**
     * @author: Pham Van Diep
     * @description: create object contract
     * @param array $row: array contract 
     * @return object | null
    */
	private function _contractToObj($row) {
		if(isset($row)) {
			$objContract			           = new stdClass();
			$objContract->id 	    	       = isset($row['contractid'])       ? $row['contractid']     : 0;
			$objContract->personalid           = isset($row['personalid'])       ? $row['personalid']     : -1;
			$objContract->clubid               = isset($row['clubid'])           ? $row['clubid']         : -1;
            $objContract->fee                  = isset($row['fee'])              ? $row['fee']: 0;
            $objContract->signed               = isset($row['signed'])           ? CI_Function::datevn($row['signed'])         : '';
            $objContract->begin                = isset($row['begin'])            ? CI_Function::datevn($row['begin'])          : '';
            $objContract->end                  = isset($row['end'])              ? CI_Function::datevn($row['end'])            : '';
			$objContract->expiry 	           = isset($row['expiry'])           ? CI_Function::datevn($row['expiry'])         : '';
			$objContract->description	       = isset($row['description'])      ? $row['description']    : '';
            $objContract->ext                  = isset($row['ext'])              ? $row['ext']            : '';
   			$objContract->status               = isset($row['status'])           ? $row['status']         : 0;         
            $objContract->parentid             = isset($row['parentid'])         ? $row['parentid']       : 0;
            
            $objContract->salary_in_week       = isset($row['salary_in_week'])   ? $row['salary_in_week'] : 0;
            $objContract->salary_unit          = isset($row['salary_unit'])      ? $row['salary_unit']    : '';
            $objContract->co_matchs            = isset($row['co_matchs'])        ? $row['co_matchs']      : 0;
            $objContract->co_matchs_t          = isset($row['co_matchs_t'])      ? $row['co_matchs_t']    : 0;
			$objContract->co_yellow_red        = isset($row['co_yellow_red'])    ? $row['co_yellow_red']  : 0;
			$objContract->co_red_card          = isset($row['co_red_card'])      ? $row['co_red_card']    : 0;
			$objContract->co_goals             = isset($row['co_goals'])         ? $row['co_goals']       : 0;
            $objContract->co_ogs               = isset($row['co_ogs'])           ? $row['co_ogs']         : 0;
			$objContract->created              = isset($row['created'])          ? $row['created']        : '';
            $objContract->modified             = isset($row['modified'])         ? $row['modified']       : '';
			$objContract->logid                = isset($row['logid'])            ? $row['logid']          : 0;
			return $objContract;
		}
		return null;
	}
    /**
     * @author: Pham Van Diep
     * @description: get contract info
     * @param integer $intId: ID
     * @return object | null
    */
	public function get($intId) {	
		$stmt = $this->_database->prepare('CALL personals_contract_get(?)');
		$stmt->bindParam(1, $intId, PDO::PARAM_INT, 11);
		$stmt->execute();
		return  $this->_contractToObj($stmt->fetch());
        //return $stmt->fetchAll();			
	}
    /**
    * @author: Pham Van Diep
    * @description: get all contracts
    * @param integer $intLoginid: user login id
	* @param integer $intPersonalId: player id
	* @param integer $intClubIdd: club id
    * @param string $strFee: Fee
    * @param string $dSigned: contract signed date
    * @param string $dBegin:  date begin contract
    * @param string $dEnd:    date end contract
    * @param string $dExpiry: contract expiry date
    * @param string $strDescription: string description
    * @param integer $intExt:
	* @param integer $intParentId:
    * @param integer $intSalaryInWeek: salary in week
	* @param string $strSalaryUnit: salary unit
    * @param array
    */	
    public function add($intLoginid, $intPersonalId,$intClubId,$strFee,$dSigned,$dBegin,$dEnd,$dExpiry,$strDescription, $intExt,$intParentId,$intSalaryInWeek,$strSalaryUnit) {
		$stmt = $this->_database->prepare('CALL personals_contract_add(?,?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1,  $intLoginid,     PDO::PARAM_INT, 11);
		$stmt->bindParam(2,  $intPersonalId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3,  $intClubId,      PDO::PARAM_INT, 11);
		$stmt->bindParam(4,  $strFee,         PDO::PARAM_STR, 20);
		$stmt->bindParam(5,  $dSigned,        PDO::PARAM_STR);
        $stmt->bindParam(6,  $dBegin,         PDO::PARAM_STR);
        $stmt->bindParam(7,  $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(8,  $dExpiry,    	  PDO::PARAM_STR);
        $stmt->bindParam(9,  $strDescription, PDO::PARAM_STR);
		$stmt->bindParam(10, $intExt,         PDO::PARAM_INT, 4);
		$stmt->bindParam(11, $intParentId,    PDO::PARAM_INT, 11);
		$stmt->bindParam(12, $intSalaryInWeek,PDO::PARAM_INT, 11);
		$stmt->bindParam(13, $strSalaryUnit,  PDO::PARAM_STR, 4);			
		$stmt->execute();	
        $result = $stmt->fetch();		
        return $this->_getErrorCode($result);	
	}    
    /**
    * @author: Pham Van Diep
    * @description: get all contracts
    * @param integer $intLoginid: user login id
    * @param integer $intStatus: status
    * @param integer $intOption: option : 0-> all
    * @param string $strKeyword 
    * @param datetime $dStart : start date
    * @param datetime $dEnd : end date 
    * @param integer  $intOffset: start of paging
    * @param integer  $intLimit: paging number 
    * @param array
    */
	
    public function getAll($intLoginid,$intStatus,$intOption,$strKeyword,$dStart,$dEnd,$intOffset,$intLimit) {
		$stmt = $this->_database->prepare('CALL personals_contract_getall(?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT);
		$stmt->bindParam(2, $intStatus,      PDO::PARAM_INT);
		$stmt->bindParam(3, $intOption,      PDO::PARAM_INT, 4);
        $stmt->bindParam(4, $strKeyword,     PDO::PARAM_STR, 50);
        $stmt->bindParam(5, $dStart,         PDO::PARAM_STR);
        $stmt->bindParam(6, $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(7, $intOffset,      PDO::PARAM_INT);
        $stmt->bindParam(8, $intLimit,       PDO::PARAM_INT, 6);	
		$stmt->execute();
		$result = $stmt->fetchAll();        
		$intTotal = isset($result[0]['foundRows']) ? $result[0]['foundRows'] : 0;
		return isset($result) ? $result : null;
	}
    /**
    * @author: Pham Van Diep
    * @final: Pham Van Diep
    * @description: delete contract
    * @param integer $intLoginid: user login id
    * @param integer $intContractid: contract id     
    * @param integer
    */
    public function delete($intLoginid,$intContractid){
        $stmt = $this->_database->prepare('CALL personals_contract_delete(?,?)');
		$stmt->bindParam(1, $intLoginid, PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intContractid,  PDO::PARAM_INT, 11);
		$stmt->execute();
        $result = $stmt->fetch();
        return $this->_getErrorCode($result);	
    }
    /**
    * @author: Pham Van Diep
    * @description: get all contracts
    * @param integer $intLoginid: user login id
	* @param integer $intContractId: contract id
	* @param integer $intPersonalId: player id
	* @param integer $intClubIdd: club id
    * @param string $strFee: Fee
    * @param string $dSigned: contract signed date
    * @param string $dBegin:  date begin contract
    * @param string $dEnd:    date end contract
    * @param string $dExpiry: contract expiry date
    * @param string $strDescription: string description
    * @param integer $intExt:
	* @param integer $intParentId:
    * @param integer $intSalaryInWeek: salary in week
	* @param string $strSalaryUnit: salary unit
    * @param array
    */	
    public function update($intLoginid,$intContractId, $intPersonalId,$intClubId,$strFee,$dSigned,$dBegin,$dEnd,$dExpiry,$strDescription, $intExt,$intParentId,$intSalaryInWeek,$strSalaryUnit) {
		$stmt = $this->_database->prepare('CALL personals_contract_update(?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
		$stmt->bindParam(1,  $intLoginid,     PDO::PARAM_INT, 11);
		$stmt->bindParam(2,  $intContractId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3,  $intPersonalId,  PDO::PARAM_INT, 11);
		$stmt->bindParam(4,  $intClubId,      PDO::PARAM_INT, 11);
		$stmt->bindParam(5,  $strFee,         PDO::PARAM_STR, 20);
		$stmt->bindParam(6,  $dSigned,        PDO::PARAM_STR);
        $stmt->bindParam(7,  $dBegin,         PDO::PARAM_STR);
        $stmt->bindParam(8,  $dEnd,           PDO::PARAM_STR);
        $stmt->bindParam(9,  $dExpiry,    	  PDO::PARAM_STR);
        $stmt->bindParam(10, $strDescription, PDO::PARAM_STR);
		$stmt->bindParam(11, $intExt,         PDO::PARAM_INT, 4);
		$stmt->bindParam(12, $intParentId,    PDO::PARAM_INT, 11);
		$stmt->bindParam(13, $intSalaryInWeek,PDO::PARAM_INT, 11);
		$stmt->bindParam(14, $strSalaryUnit,  PDO::PARAM_INT, 11);					
		$stmt->execute();	
        $result = $stmt->fetch();		
        return $this->_getErrorCode($result);
	}
    /**
    * @author: Pham Van Diep
    * @description: update status of contract
    * @param integer $intLoginid: user login id
    * @param integer $intContractid: contract id
    * @param integer $intStatus: contract status
    * @param integer $intPerm: permission      
    * @param integer
    */
    public function updateStatus($intLoginid,$intContractid,$intStatus,$intPerm){
        $stmt = $this->_database->prepare('CALL personals_contract_update_status(?,?,?,?)');
		$stmt->bindParam(1, $intLoginid,     PDO::PARAM_INT, 11);
        $stmt->bindParam(2, $intContractid,  PDO::PARAM_INT, 11);
		$stmt->bindParam(3, $intStatus,      PDO::PARAM_INT, 4);
		$stmt->bindParam(4, $intPerm,        PDO::PARAM_INT, 4);							
		$stmt->execute();	
        $result = $stmt->fetch();
        return $this->_getErrorCode($result);
    }	
	/**
	*	@description: get Error code from query result
	*	@param: mixed query result
	*	@return: int
	*/
	private function _getErrorCode ( $tableData ) {
		return isset($tableData['errCode'])?intval($tableData['errCode']):-1;
	}	
	
}